********************************************************************************
** 	TITLE:   f1_mt_morris_test_taker_representative.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Table III: Representativeness of Test Takers

********************************************************************************
		set sortseed 13
				
		use "$output_data/estimate.dta", clear
		capture drop last 
		capture drop first
		gen last = substr(fullname,1,strpos(fullname,",")-1) 
		gen first = subinstr(fullname, last,"",.)
		replace first = subinstr(trim(first),",","",.)
		replace first = substr(trim(first),1,strpos(trim(first)," ")-1) ///
			if strpos(trim(first)," ")
			
/*********************************************************************************
**********************************************************************************/
	est clear
capture drop _merge
		merge m:1 fullname dob using "$output_data/mt_morris_eudc.dta", force keep(1 3)
		
		gen has_student_id = !mi(student_id)
		bys inmate (booking_date): replace has_student_id=0 if _n!=_N

		
		destring reading_pre reading_post math_pre math_post, replace force

		
		bys inmate (booking_date): gen last_book = _n==_N
		
		gen has_test_score = !mi(math_pre) &!mi(math_post) ///
			&!mi(reading_pre)&!mi(reading_post) 
		
		//Keeping those that can link to Mt. Morris Schools data
		keep if has_student_id == 1

		**Balance Table
		est clear
		
		local zipvar  pctebll pc_black pc_hs log_med_hhincome
				reghdfe has_test_score D_female age_17_24 age_25_34 age_35_44 age_45_54 age_55_64 age_65plus D_Black D_public_defender `zipvar' ///
					, vce(robust)  a($design_control)
				eststo bal_joint1
				testparm D_female age_17_24 age_25_34 age_35_44 age_45_54 age_55_64 age_65plus D_Black D_public_defender `zipvar'
				local fp = r(p)
				local fp: di %8.3f `fp'
				local fp = subinstr("`fp'"," ","",.)
				
				local fs = r(F)
				local fs: di %8.3f `fs'
				local fs = subinstr("`fs'"," ","",.)
				local fs = " \multicolumn{3}{c}{`fs' [`fp']}"
				estadd local Fs "`fs'": bal_joint1

			local Num = e(N)
			local Num : display %8s `Num'
			local clust = e(N_clust)
			local clust : display %8s `clust'
			
			local Num = "\multicolumn{3}{c}{`Num'}"
			local clust = "&\"+"multicolumn{2}{c}{`clust'}"
			local cont = "&\"+"multicolumn{2}{c}{Yes}"
			
			
			estadd local Num "`Num'" : bal_joint1
			estadd local clust "`clust'" : bal_joint1
			estadd local cont "`cont'": bal_joint1
			
		foreach v of varlist D_female age_17_24 age_25_34 age_35_44 age_45_54 age_55_64 age_65plus D_Black D_public_defender {
			
			
			reghdfe `v' has_test_score if `v'>=0, vce(robust) a($design_control)
			local `v'_b =_b[has_test_score]
			local `v'_se = _se[has_test_score]
			
			local `v'_b: display %8.3f ``v'_b'
			local `v'_se: display %8.3f ``v'_se'
			
			mat test = r(table)
			local p_val =  el(test,4,1)
				if `p_val'<0.01 {
					local star1="***"
				}
				if `p_val'>=0.01 & `p_val'<0.05 {
					local star1="**"
				}
				if `p_val'>=0.05 & `p_val'<0.1 {
					local star1="*"
				}
				if `p_val'>=0.1 {
					local star1=""
				}
			
			local `v'_b = "``v'_b'"+"`star1'"
			local `v'_se = subinstr("(``v'_se')"," ","",.)
				
			estadd local `v'_b "``v'_b'" : bal_joint1
				
				
				su `v' if e(sample)==1
				
				local `v'_mean = r(mean)
				local `v'_sd = r(sd)
				local `v'_mean: display %8.3f ``v'_mean'
				local `v'_sd: display %8.3f ``v'_sd'
				local `v'_sd = subinstr("(``v'_sd')"," ","",.)
				
				local `v'_se = "&"+ "``v'_se'"
				local `v'_diff = "``v'_mean'" + "&" + "``v'_b'" + "``v'_se'"
				
				estadd local `v'_diff "``v'_diff'" : bal_joint1
				estadd local `v'_se "``v'_se'" : bal_joint1
		}
		
			foreach v in `zipvar' {
				reghdfe `v' has_test_score `cond' , vce(robust) a($design_control)
				local `v'_b =_b[has_test_score]
				local `v'_se = _se[has_test_score]
				
				local `v'_b: display %8.3f ``v'_b'
				local `v'_se: display %8.3f ``v'_se'
				
				mat test = r(table)
				local p_val =  el(test,4,1)
					if `p_val'<0.01 {
						local star1="***"
					}
					if `p_val'>=0.01 & `p_val'<0.05 {
						local star1="**"
					}
					if `p_val'>=0.05 & `p_val'<0.1 {
						local star1="*"
					}
					if `p_val'>=0.1 {
						local star1=""
					}
				
				local `v'_b = "``v'_b'"+"`star1'"
				local `v'_se = subinstr("(``v'_se')"," ","",.)
				
				
				
				
				su `v' if e(sample)==1&`v'>=0
				
				local `v'_mean = r(mean)
				local `v'_sd = r(sd)
				local `v'_mean: display %8.3f ``v'_mean'
				local `v'_sd: display %8.3f ``v'_sd'
				local `v'_sd = subinstr("(``v'_sd')"," ","",.)
				
				
				
				local `v'_se = "&"+ "``v'_se'"
				
				local `v'_diff = "``v'_mean'" + "&" + "``v'_b'" + "``v'_se'"
				estadd local `v'_diff "``v'_diff'" : bal_joint1
				estadd local `v'_se "``v'_se'" : bal_joint1
				}
		
		estout bal_joint1 ///
						using "$tables/test_taker_representative.tex", style(tex)  ///
						label nobaselevels collabels(none) mlabels(none) /// 
						drop(D_female age_17_24 age_25_34 age_35_44 age_45_54 age_55_64 age_65plus D_Black D_public_defender pctebll pc_black pc_hs log_med_hhincome _cons) ///
						cells(b(star fmt(%8.3f)) se(par fmt(%8.3f))) /// 
						starlevels(* 0.10 ** 0.05 *** 0.01) /// 
						stats(foo D_female_diff age_17_24_diff age_25_34_diff age_35_44_diff age_45_54_diff age_55_64_diff ///
							age_65plus_diff D_Black_diff D_public_defender_diff ///
							foo foo pctebll_diff pc_black_diff pc_hs_diff log_med_hhincome_diff ///
							foo Fs Num, /// 
						labels("\textbf{Panel A: Individual Characteristics}"  "Female" "Age 18-24" "Age 25-34" "Age 35-44" "Age 45-54" "Age 55-64" "Age 65+" ///
						"Black" "Has a Public Defender" ///
						" " "\textbf{Panel B: Census Tract Characteristics}" "Share with Elevated Blood Lead Level"  ///
						"Share Black" "Share High School Graduate or Higher" "Log Median Household Income" ///
						\hline "\emph{F}-Statistic for Joint Test [\emph{p}-value]" ///
						"Observations")) replace